const app = getApp()
const { URL } = require('../../utils/api.js')
import { formatDate, showError } from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabBar: [
      {
        title: '全部订单'
      },
      {
        title: '成功订单'
      },
      {
        title: '失败订单'
      },
    ],
    current_tab: 0,
    orderList:[],  // 订单列表
    currentPage:1,
    pageSize:50,
    orderCode:'', // 订单号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserOrderList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 获取用户订单列表
  getUserOrderList(){
    wx.showLoading({
      title: '加载中',
    }) 
    let data = {
        currentPage: this.data.currentPage,
        pageSize: this.data.pageSize,
        value: this.data.current_tab
    }
    app._post(URL.userOrders, data, res => {
      if (res.data.code === '00') {
        for (let i = 0; i < res.data.result.length; i++) {
          res.data.result[i]['createdTime'] = formatDate(res.data.result[i]['createdTime'], 2)
        }
        this.setData({
          orderList: res.data.result
        })
      }
      else{
        this.setData({
          orderList:[]
        })
      }
    })
  },
  // tab切换
  switchTabBar(e){
    let index = e.detail.value;
    this.setData({
      current_tab:index
    })
    this.getUserOrderList();
  },
  // 重新出货
  redoAction(e){
    let that = this;
    let orderCode = e.currentTarget.dataset.ordercode;
    let timer = setInterval(() => {
      let result = that.getPayResult(orderCode);
      if (result) {
        clearInterval(timer);
        wx.navigateTo({
          url: '/pages/blueToothPay/index',
        })
      }
    }, 1000)
    
  },
  // 申请退款
  refundMoney(e){
    let orderCode = e.currentTarget.dataset.ordercode;
    this.setData({
      orderCode: e.currentTarget.dataset.ordercode
    })
    showError('', '是否确认退款?', '否', '是', this.doRefundAction);
  },
  // 退款操作
  doRefundAction(orderCode){
    let that = this;
    let data = {
      value:{
        orderCode:that.data.orderCode
      }
    }
    app._post(URL.applyRefund,data,res=>{
      if (res.code === '00'){
        that.refundRemind();
      }
    })
  },
  // 退款后提醒
  refundRemind() {
    showError('', '退款成功,正在退款中,预计3天内退款成功', '关闭', '重新购买', function () {
      wx.navigateBack({
        delta: 1
      })
    });
  },
  // 获取支付接口，和数据库校验的结果
  getPayResult(orderCode) {
    return new Promise((resolve, reject) => {
      app._get(URL.orderCheck + orderCode, {}, res => {
        if (res.data.code === '00') {
          let index = res.data.result.terminalCode.indexOf("y");
          app.globalData.terminalCode = res.data.result.terminalCode.substring(index + 1),  // 设备名
          app.globalData.message = res.data.result.message             // 向蓝牙发送的数据
          resolve(true);
        }
        else {
          resolve(false);
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})